<?php

namespace App\Admin\Extensions\Tools;

use App\Models\Message;
use App\Models\Topic;
use Dcat\Admin\Grid\BatchAction;
use Illuminate\Http\Request;

class CheckTopic extends BatchAction
{
    protected $action;
    protected $style = 'btn btn-white waves-effect';

    // 注意action的构造方法参数一定要给默认值
    public function __construct($title = null, $action = 1)
    {
        $this->title = $title;
        $this->action = $action;
    }

    // 确认弹窗信息
    public function confirm()
    {
        return $this->action == 1 ? '是否审核通过？' : '是否审核不通过？';
    }

    // 处理请求
    public function handle(Request $request)
    {
        // 获取选中的文章ID数组
        $keys = $this->getKey();

        // 获取请求参数
        $action = $request->get('action');

        foreach (Topic::find($keys) as $post) {
            $post->status = $action;
            $post->save();
            Message::insert([
                'title' => '审核通知',
                'type' => 2,
                'admin_id' => 0,
                'member_id' => $post->member_id,
                'content' => $action == 1 ? '您的帖子已经审核通过' : '您的帖子审核不通过',
                'created_at' => date('Y-m-d H:i:s')
            ]);
        }

        $message = '操作成功';

        return $this->response()->success($message)->refresh();
    }

    // 设置请求参数
    public function parameters()
    {
        return [
            'action' => $this->action,
        ];
    }
}
